package com.ychs.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ychs.entity.College;
import com.ychs.service.CollegeService;
import com.ychs.service.IBaseService;
import com.ychs.util.Constants;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/14
 */
@WebServlet(value = "/CollegeServlet")
public class CollegeServlet extends BaseServlet {
    private IBaseService<College> service = new CollegeService();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter add method");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        College college = new College();
        college.setName(name);
        college.setCode(Integer.parseInt(code));
        boolean result = service.add(college);
        logger.info("result : " + result);

        //request.getRequestDispatcher("page/college/collegeManager.jsp").forward(request,response);
        //response.sendRedirect("/uolab/index.jsp#/page/college/collegeManager.jsp");

        //print不等于println，后者比前者多了两个字符（换行的字符）
        response.getWriter().print(result);
    }

    @Override
    protected void toModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter modify method");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        College college = new College();
        college.setId(Integer.parseInt(id));
        college.setCode(Integer.parseInt(code));
        college.setName(name);
        boolean result = service.modify(college);
        logger.info("result =" + result);
        response.getWriter().print(result);
    }

    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter remove method");
        String id = request.getParameter("id");
        boolean result = service.remove(Integer.parseInt(id));
        logger.info(result);
        response.getWriter().print(result);
    }

    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter search method");

        Map<String, Object> conditions = new HashMap<>();
        String pageStr = request.getParameter("page");
        String limitStr = request.getParameter("limit");

        //获取满足条件的总条数
        int count = service.searchTotalNumber(conditions);

        if (pageStr != null || limitStr != null) {
            int begin = (Integer.parseInt(pageStr) - 1) * Integer.parseInt(limitStr);
            conditions.put("begin", begin);
            conditions.put("pageSize", Integer.parseInt(limitStr));
        }

        //查询数据
        List<College> colleges = service.search(conditions);
        //准备Json
        String json = json(count,colleges);
        logger.info(json);
        //把Java字符串输出到前端
        response.getWriter().print(json);
    }

    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter searchById method");
        request.setCharacterEncoding("utf-8");
        //查询数据
        String idStr = request.getParameter("id");
        College result = service.searchById(Integer.parseInt(idStr));
        ObjectMapper mapper = new ObjectMapper();
        //把java对象转换为JSON字符串
        String json = mapper.writeValueAsString(result);
        logger.info("json" + json);
        //把JSON字符串输出到前端
        response.getWriter().write(json);
    }

    @Override
    protected void verify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter verify method");
        Map<String, Object> conditions = new HashMap<>();
        String data = request.getParameter("data");
        logger.info(data);
        conditions.put("name",data);
        int count = service.searchTotalNumber(conditions);
        logger.info(count);
        boolean result;
        if (count == Constants.NOT_EXIST) {
            result=true;
        }else{
            result=false;
        }
        response.getWriter().print(result);
    }
}
